//
//  AssessTableViewCell.m
//  美食百科
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "AssessTableViewCell.h"
#import "ClassModel.h"
@interface AssessTableViewCell()
@property(nonatomic,retain)UIImageView *opinionView;
@property(nonatomic,retain)UILabel *opinionLable;
@property(nonatomic,retain)UILabel *assessLable;


@end
@implementation AssessTableViewCell

- (void)dealloc
{
    [_opinionView release];
    [_opinionLable release];
    [_assessLable release];
    [super dealloc];
    
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        self.opinionView = [[UIImageView alloc]init];
        //self.opinionView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:self.opinionView];
        [_opinionView release];
        
        self.opinionLable  = [[UILabel alloc]init];
        self.opinionLable.textColor = self.color;
        [self.contentView addSubview:self.opinionLable];
        self.opinionLable.textAlignment = NSTextAlignmentCenter;

       // self.opinionLable.backgroundColor = [UIColor blueColor];

        [_opinionLable release];
        
        self.assessLable  = [[UILabel alloc]init];
       // self.assessLable.backgroundColor = [UIColor redColor];
        self.assessLable.numberOfLines = 0;
        self.assessLable.textColor = self.color;

        [self.contentView addSubview:self.assessLable];
        [_assessLable release];
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.opinionView.frame = CGRectMake(10, 10, self.bounds.size.width/5.5, self.bounds.size.width/5.5);
    
    self.opinionLable.frame = CGRectMake(self.opinionView.frame.origin.x, self.opinionView.frame.origin.y+self.opinionView.frame.size.height, self.opinionView.frame.size.width, self.opinionView.frame.size.height/2);
    
CGFloat Height = [AssessTableViewCell heightOfSelfSuit:self.classModel.appraise font:self.assessLable.font width:self.bounds.size.width-(self.opinionView.frame.origin.x+self.opinionView.frame.size.width+10)-10];
    
    self.assessLable.frame = CGRectMake(self.opinionView.frame.origin.x+self.opinionView.frame.size.width+10, 0, self.bounds.size.width-(self.opinionView.frame.origin.x+self.opinionView.frame.size.width+10)-10, Height);
}

-(void)setClassModel:(ClassModel *)classModel
{
    if (_classModel != classModel) {
        [_classModel release];
        _classModel  = [classModel retain];
    }
    NSInteger health_light = [classModel.health_light integerValue];
    if (health_light ==1) {
        self.opinionView.image = [UIImage imageNamed:@"assess1"];
        self.opinionLable.text = @"推荐";
        self.opinionLable.textColor = [UIColor colorWithRed:0.067 green:0.804 blue:0.431 alpha:1];
    }else if (health_light ==2){
        self.opinionView.image = [UIImage imageNamed:@"assess2"];
        self.opinionLable.text = @"适量";
        self.opinionLable.textColor = [UIColor colorWithRed:0.957 green:0.776 blue:0.000 alpha:1];
    }else{
        self.opinionView.image = [UIImage imageNamed:@"assess3"];
        self.opinionLable.text = @"少吃";
        self.opinionLable.textColor = [UIColor colorWithRed:0.922 green:0.310 blue:0.220 alpha:1];
    }
    
    if (classModel.appraise == nil) {
        self.assessLable.text = @"暂无评价";
    }else{
        self.assessLable.text = classModel.appraise;
   
    }
    
}

+(CGFloat)heightOfSelfSuit:(NSString *)text font:(UIFont *)font width:(CGFloat)width
{
    
    //第一步：设置文本显示的范围
    //如果计算label的自适应高度，那么宽度需要固定，高度需要尽量设置大值；
    
    CGSize size = CGSizeMake(width, MAXFLOAT);
    
    //第二步：设置要显示的文本样式
    NSDictionary *style = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
    
    //第三步：根据文本内容和文本样式计算label的frame
    /**
     *  参数说明：1.文本显示范围；2.文本显示规则；3.文本样式（通常只包括字体）4.通常nil.
     */
    CGRect result = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:style context:nil];
    
    //返回自适应之后的高度
    
    if (result.size.height <([UIScreen mainScreen].bounds.size.width/5.5*1.5+10)) {
        return [UIScreen mainScreen].bounds.size.width/5.5*1.5+10;
    }else{
        return result.size.height;
    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
